package com.example.mr5.bluetoothppt;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.mr5.bluetoothppt.Util.SocketUtil;

import java.io.IOException;
import java.util.HashMap;

public class ControlActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageButton next;
    private ImageButton upon;
    private ImageButton disconnect;
    private ImageButton fullscreen;
    private HashMap<String, String> device;
    private final static String DATA_UPON = "1";
    private final static String DATA_NEXT = "2";
    private final static String DATA_FULL = "3";
    private final static String DATA_EXIT_FULL = "4";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_control);
        device = (HashMap<String, String>) getIntent().getSerializableExtra("device");
        initView();
    }

    private void initView() {
        next = (ImageButton) findViewById(R.id.next);
        upon = (ImageButton) findViewById(R.id.upon);
        disconnect = (ImageButton) findViewById(R.id.disconnect);
        fullscreen = (ImageButton) findViewById(R.id.fullscreen);
        next.setOnClickListener(this);
        upon.setOnClickListener(this);
        disconnect.setOnClickListener(this);
        fullscreen.setOnClickListener(this);
    }

    private boolean isFullScreen = false;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.next:
                sendData(DATA_NEXT);
                break;
            case R.id.upon:
                sendData(DATA_UPON);
                break;
            case R.id.disconnect:
                SocketUtil.close();
                finish();
                break;
            case R.id.fullscreen:
                if(isFullScreen){
                    sendData(DATA_EXIT_FULL);
                    fullscreen.setImageResource(R.drawable.ic_fullscreen_exit);
                }else {
                    sendData(DATA_FULL);
                    fullscreen.setImageResource(R.drawable.ic_fullscreen_black);
                }
                isFullScreen = !isFullScreen;
                break;

        }
    }

    private void sendData(String s){
        if (!SocketUtil.write(s.getBytes())) {
            displayLongToast("数据通道未建立,发送失败");
        }
    }


    public void displayLongToast(String str) {
        Toast toast = Toast.makeText(this, str, Toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP, 0, 220);
        toast.show();
    }
}
